
const util = require('./../utils/util.js')

//
const make_totp_secret = function () {
	var t = util.uuid().replace(/\-/g, '');
	return t;
}
//
const totp_config = function () {
	this.secretList = null;
	this.secretDefault = null;
	const totp_secret_list = "totp_secret_list";
	const totp_secret_default = "totp_secret_default";
	// 加载和保存
	this.saveConfig = function () {
		wx.setStorageSync(totp_secret_list, this.secretList);
		wx.setStorageSync(totp_secret_default, this.secretDefault);
	};
	this.loadConfig = function () {
		if (this.secretList == null) {
			this.secretList = wx.getStorageSync(totp_secret_list) || [];
			this.secretDefault = wx.getStorageSync(totp_secret_default);
		}
	}
	//
	this.filterName = function (name) {
		if (name)
			return name;
		if (this.secretDefault)
			return this.secretDefault;
		if (this.secretList.length > 0) {
			this.secretDefault = this.secretList[0].name;
			return this.secretDefault;
		}
		return null;
	}
	this.calcSecretMarked = function (secret) {
		if (secret && secret.length > 8) {
			return secret.substring(0, 4)
				+ "****"
				+ secret.substring(secret.length - 2);
		} else {
			return "****";
		}
	}
	//
	this.getSecretList = function () {
		return this.secretList;
	};
	this.getSecret = function (name) {
		name = this.filterName(name);
		for (var i in this.secretList) {
			if (this.secretList[i].name === name) {
				return this.secretList[i];
			}
		}
		return null;
	};
	this.initAndGetSecret = function (name) {
		name = this.filterName(name);
		var secret = make_totp_secret();
		return this.setSecret(name, secret, 90);
	};
	this.getOrInitSecret = function (name) {
		name = this.filterName(name);
		var ret = this.getSecret(name);
		if (ret == null)
			ret = this.initAndGetSecret(name);
		return ret;
	};
	this.rename = function (name_old, name_new) {
		for (var i in this.secretList) {
			if (this.secretList[i].name === name_old) {
				this.secretList[i].name = name_new;
				return this.secretList[i];
			}
		}
		return false;
	},
	this.updateSecret = function(name_old, name_new, secret, ttl) {
		name_old = this.filterName(name_old);
		for (var i in this.secretList) {
			if (this.secretList[i].name === name_old) {
				if (name_new) {
					this.secretList[i].name = name_new;
				}
				if (secret) {
					this.secretList[i].secret = secret;
					this.secretList[i].secret_marked = this.calcSecretMarked(secret);
					var date = new Date();
					this.secretList[i].time = date.getTime();
					this.secretList[i].timeStr = util.formatTime(date);
				}
				if (ttl) {
					this.secretList[i].ttl = ttl;
				}
				return this.secretList[i];
			}
		}
	}
	this.addSecret = function (name, secret, ttl) {
		ttl = ttl || 90;
		name = this.filterName(name);
		if (this.getSecret(name) == null) {
			var secret_marked = this.calcSecretMarked(secret);
			var date = new Date();
			var config = {
				name: name,
				secret: secret,
				secret_marked: secret_marked,
				time: date.getTime(),
				timeStr: util.formatTime(date),
				ttl, ttl
			};
			this.secretList.push(config);
			return config;
		} else {
			return null;
		}
	}
	this.setSecret = function (name, secret, ttl) {
		var ret = this.updateSecret(name, null, secret, ttl);
		if (ret == null) {
			return this.addSecret(name, secret, ttl);
		}
	};
	this.deleteSecret = function (name) {
		for (var i in this.secretList) {
			if (this.secretList[i].name === name) {
				this.secretList.splice(i, 1);
				return 1;
			}
		}
		return 0;
	};
	this.setSecretTop = function(name) {
		var secret = this.getSecret(name);
		this.deleteSecret(name);
		this.secretList.splice(0, 0, secret);
	};

	// 加载数据
	this.loadConfig();
}

//
module.exports = {
	make_totp_secret: make_totp_secret,
	totp_config: new totp_config(),
}
